const {
    FOUNDATION_PREFIX
} = require('../config')
const request = require('request-promise')
const {
    SERVICE_TOKEN
} = require('../config')

const TRANSFORM = function(buffer, res) {
    return res.body.data
}


exports.userProxy = function(path, options = {}) {
    const method = options.method || 'GET'
    const qs = options.qs
    const body = options.body
    const transform = options.transform || TRANSFORM
    const headers = options.headers

    const settings = {
        headers,
        method,
        qs,
        body,
        transform,
        json: true
    }
    return new Promise(function(resolve, reject) {
        request(`${FOUNDATION_PREFIX}/${path}`, settings)
            .then(function(res) {
                resolve(res)
            })
            .catch(function(err) {
                reject(err)
            })
    })
}


exports.serviceProxy = function(path, options = {}) {
    if (options.headers) {
        Object.assign(options.headers, {
            token: SERVICE_TOKEN
        })
    } else {
        options.headers = {
            token: SERVICE_TOKEN
        }
    }

    return exports.userProxy(path, options)
}